﻿using System.Drawing;

var converter = new ObjectToTextConverter();
converter.Conver(
    new House("110 Castle Road", 23.4, 4));


var point = new Point(3, 5);
var anotherPoint = point;
anotherPoint.Y = 100;
Console.WriteLine("point is:"+point);
Console.WriteLine("anotherPoint is:" + anotherPoint);

Console.ReadKey();

class ObjectToTextConverter
{
    public string Conver(object obj)
    {
        Type type = obj.GetType();
        var properties = type
            .GetProperties()
            .Where(p => p.Name != "EqualityContract");

        return string.Join(
            ", ",
            properties
                .Select(property =>
                    $"{property.Name} is {property.GetValue(obj)}"));
    }
}

public record Pet(string Name, PetType PetType, float Weight);

public record House(string Address, double Area, int Floors);

public enum PetType
{
    Cat,
    Dog,
    Fish
}

public class Person
{
    [StringLengthValidate(2,25)]
    public string Name { get; }
    public int YearOfBirth { get; }

    public Person(string name, int yearOfBirth)
    {
        Name = name;
        YearOfBirth = yearOfBirth;
    }

    public Person(string name) => Name = name;
}


[AttributeUsage(AttributeTargets.Property)]
class StringLengthValidateAttribute : Attribute
{
    public int Max { get; }
    public int Min { get; }

    public StringLengthValidateAttribute(int max, int min)
    {
        Max = max;
        Min = min;
    }
}

class Validator
{
    public bool Validate(object obj)
    {
        var type = obj.GetType();
        var propertiesToValidate = type
            .GetProperties()
            .Where(property =>
                Attribute.IsDefined(property, typeof(StringLengthValidateAttribute)));
        
        foreach (var property in propertiesToValidate)
        {
            object? propertyValue = property.GetValue(obj);
            if (propertyValue is not string)
            {
                throw new InvalidOperationException(
                    $"Attribute {nameof(StringLengthValidateAttribute)}" +
                    $" can only be applied to strings");
            }

            var value = (string)propertyValue;
            var attribute = (StringLengthValidateAttribute)
                property.GetCustomAttributes(
                    typeof(StringLengthValidateAttribute), true).First();
            if (value.Length < attribute.Min || value.Length > attribute.Max)
            {
                Console.WriteLine($"Property {property.Name} is invalid." +
                                  $" Value is {value}");
                return false;
            }
        }

        return true;
    }
}